package com.vainycos.lotterysearch.controller;

import com.vainycos.lotterysearch.model.BigLotteryBall;
import com.vainycos.lotterysearch.model.base.Result;
import com.vainycos.lotterysearch.model.ro.BigLotteryRo;
import com.vainycos.lotterysearch.service.BigLotteryService;
import com.vainycos.lotterysearch.service.notify.DingNotify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: Vainycos
 * @description 大乐透查询
 * @date: 2022/11/3 13:42
 */
@RestController
@RequestMapping("/bigLottery")
public class BigLotteryController {

    @Autowired
    private BigLotteryService bigLotteryService;

    @Autowired
    private DingNotify dingNotify;

    /**
     * 查询某一期之后所有的开奖情况
     * @param periodId
     * @throws Exception
     */
    @GetMapping("/searchBallWithinPeriodId")
    public Result<List<BigLotteryBall>> searchBallWithinPeriodId(int periodId) {
        List<BigLotteryBall> list= bigLotteryService.getLaterBLB(periodId);
        return Result.of(list);
    }

    /**
     * 根据期数查询单期开奖情况
     * @param periodId
     */
    @GetMapping("/searchBallBySinglePeriodId")
    public Result<BigLotteryBall> searchBallBySinglePeriodId(int periodId) {
        return Result.of(bigLotteryService.getBLBBySinglePeriodId(periodId));
    }

    /**
     * 查询最近一期开奖情况
     */
    @GetMapping("/searchLatest")
    public Result<BigLotteryBall> searchLatest() {
        return Result.of(bigLotteryService.searchLatestBigLottery());
    }


    /**
     * 输入号码匹配最近一期中奖情况
     * @param bigLotteryRo 输入号码
     * @return
     */
    @PostMapping("/matchingResultWithLatest")
    public Result<String> matchingResultWithLatest(@RequestBody @Valid BigLotteryRo bigLotteryRo) {
        StringBuffer result = new StringBuffer();
        BigLotteryBall latestResult = bigLotteryService.searchLatestBigLottery();
        result.append("期号：" + latestResult.getPeriodId() + "。"
                + "开奖日期：" + latestResult.getLotteryDate() + "。"
                + "开奖号码：" + latestResult.winnerResult() + "。"
                + "您的号码：" + bigLotteryRo.inputNumberResult() + "。");
        int i = bigLotteryService.matchLevelPrizes(bigLotteryRo, latestResult);
        if (i > 0){
            result.append("恭喜您中奖了，中了" + i + "等奖");
        }else {
            result.append("不好意思您未中奖，希望下次好运");
        }
        return Result.of(result.toString());
    }

    /**
     * 输入号码匹配最近一期中奖情况
     * @param bigLotteryRo 输入号码
     * @return
     */
    @PostMapping("/matchingResultWithPeriodId")
    public Result<String> matchingResultWithPeriodId(@RequestBody @Valid BigLotteryRo bigLotteryRo) {
        return Result.of(bigLotteryService.describeByPrice(bigLotteryService.getBLBBySinglePeriodId(bigLotteryRo.getPeriodId()), bigLotteryRo));
    }

    /**
     * 全量初始化新增
     * @return
     */
    @GetMapping("/batchInsertInit")
    public Result batchInsertInit(){
        return Result.of(bigLotteryService.batchInsertInit());
    }

    /**
     * 同步增量更新
     * @return
     */
    @GetMapping("/syncInsert")
    public Result syncInsert(){
        return Result.of(bigLotteryService.syncInsert());
    }

    /**
     * 前区号码匹配历史中奖率
     * @param frontAreaNum 前区号码
     * @return
     */
    @GetMapping("/historyFrontArea")
    public Result historyFrontArea(int frontAreaNum){
        String result = "";
        String s = bigLotteryService.historyFrontArea(frontAreaNum);
        result += frontAreaNum + "->该前区号码历史中奖率为" + s;
        return Result.of(result);
    }

    /**
     * 后区号码匹配历史中奖率
     * @param endAreaNum 后区号码
     * @return
     */
    @GetMapping("/historyEndArea")
    public Result historyEndArea(int endAreaNum){
        String result = "";
        String s = bigLotteryService.historyEndArea(endAreaNum);
        result += endAreaNum + "->该后区号码历史中奖率为" + s;
        return Result.of(result);
    }

    /**
     * 统计前区1-35数字的历史中奖率
     */
    @GetMapping("/statisticsFront")
    public Result statisticsFront(){
        String result = "";
        for (int i = 1; i < 36; i++) {
            String s = bigLotteryService.historyFrontArea(i);
            result += i + "->该前区号码历史中奖率为" + s + " \n";
        }
        return Result.of(result);
    }

    /**
     * 统计后区1-12数字的历史中奖率
     */
    @GetMapping("/statisticsEnd")
    public Result statisticsEnd(){
        String result = "";
        for (int i = 1; i < 13; i++) {
            String s = bigLotteryService.historyEndArea(i);
            result += i + "->该后区号码历史中奖率为" + s + " \n";
        }
        return Result.of(result);
    }

    @GetMapping("/dingNotifyStatistics")
    public Result dingNotifyStatistics(){
        String result = "超级大乐透历史号码统计\n";
        for (int i = 1; i < 36; i++) {
            String s = bigLotteryService.historyFrontArea(i);
            result += i + "->该前区号码历史中奖率为" + s + " \n";
        }
        result += "--- \n";
        for (int i = 1; i < 13; i++) {
            String s = bigLotteryService.historyEndArea(i);
            result += i + "->该后区号码历史中奖率为" + s + " \n";
        }
        dingNotify.notifyMarkdown("超级大乐透历史号码统计", result);
        return Result.of(true);
    }
}

